/*
 * CommentsDao.java
 *
 * Created on November 9, 2008, 7:34 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Comments;

/**
 *
 * @author JollkY
 */
public class CommentsDao implements Crud {

    private int state;
    private Comments comment;
    private List comments;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();

    /**
     * Creates a new instance of CommentsDao
     */
    public CommentsDao() {
    }

    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }


    public List<Object> getList() {
        comments = new ArrayList();
        try {
            em.getTransaction().begin();
            comments = em.createQuery("SELECT comment FROM Comments comment").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return comments;
    }
    
    public List getListByCommentator(long id) {
        comments = new ArrayList();
        try {
            em.getTransaction().begin();
            comments = em.createQuery("SELECT c FROM Comments c " +
                    "WHERE c.commentator.id="+id).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return null;
    }

    public List getListByKnowledgeBase(long id) {
        this.comments = new ArrayList();
        try {
            em.getTransaction().begin();
            this.comments = em.createQuery("SELECT comments FROM Comments comments " +
                    "WHERE comments.knowledgeBase.id=" + id).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.comments;
    }

    public List getListByComment(long id) {
        this.comments = new ArrayList();
        try {
            em.getTransaction().begin();
            this.comments = em.createQuery("SELECT comments FROM Comments comments " +
                    "WHERE comments.replyFor.id=" + id).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.comments;
    }

    public Object read(long id) {
        this.comment = new Comments();
        try {
            em.getTransaction().begin();
            this.comment = em.find(Comments.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return this.comment;
    }

    public int update(Object obj) {
        this.comment = new Comments();
        try {
            Comments objComment = (Comments) obj;
            em.getTransaction().begin();
            this.comment = em.find(Comments.class, objComment.getId());
            comment.setComment(objComment.getComment());
            comment.setCommentator(objComment.getCommentator());
            comment.setDatePost(objComment.getDatePost());
            comment.setDatePublish(objComment.getDatePublish());
            comment.setDateUpdate(objComment.getDateUpdate());
            comment.setKnowledgeBase(objComment.getKnowledgeBase());
            comment.setReplyFor(objComment.getReplyFor());
            comment.setStatus(objComment.getStatus());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }

    public int delete(long id) {
        this.comment = new Comments();
        try {
            em.getTransaction().begin();
            this.comment = em.find(Comments.class, id);
            em.remove(this.comment);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return this.state;
    }
}
